package com.liuya.safe.policy.handler;

import com.liuya.base.BaseModel;
import com.liuya.common.WebUtil;
import com.liuya.safe.policy.service.QueryManagerService;
import com.liuya.safe.model.SafeQuery;
import com.liuya.safe.tree.AbstractTreeHandler;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 策略查询树处理类
 */
@Component("queryTreeHandler")
public class QueryTreeHandler extends AbstractTreeHandler {

    @Resource(name="queryManagerService")
    private QueryManagerService queryService;

    @Override
    public String getPageTitle() {
        return "Query_Tree";
    }

    @Override
    public String getCreatePage() {
        return "policy/query/create";
    }

    @Override
    public String getNodeName(BaseModel node) {
        SafeQuery query = (SafeQuery) node;
        return query.getName();
    }

    /**
     * 获取所有节点
     * @return
     */
    @Override
    public Collection getAllNodes() {
        return queryService.getAllQueries();
    }

    @Override
    public BaseModel getNode(HttpServletRequest req) {
        SafeQuery query = new SafeQuery();
        String id = getId(req);
        String pid = getParentId(req);
        String name = req.getParameter("name");
        String desc = req.getParameter("description");
        boolean isLeaf = WebUtil.getBooleanParameter(req, "isLeaf", false);

        query.setId(id);
        query.setIsLeaf(isLeaf);
        query.setName(name);
        query.setPid(pid);
        query.setDescription(desc);
        return query;
    }

    @Override
    public BaseModel getNodeByName(String name) {
        Collection nodes = queryService.getAllQueries();
        for (Iterator iter = nodes.iterator(); iter.hasNext();) {
            SafeQuery node = (SafeQuery) iter.next();
            if (node.getName().equals(name)) {
                return node;
            }
        }
        return null;
    }

    @Override
    public boolean addNode(Object node) {
        SafeQuery query = (SafeQuery) node;
        try{
            queryService.addQuery(query.getPid(), query.getName(), query.getDescription(), query.getIsLeaf());
            return true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteNode(String id) {
        queryService.deleteQuery(id);
        return true;
    }

    @Override
    public boolean updateNode(Object node) {
        SafeQuery db = (SafeQuery) node;
        try{
            queryService.updateQuery(db.getId(), db.getName(), db.getDescription());
        } catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public boolean moveNode(String parentId, String id, int position) {
        queryService.moveQuery(id, parentId);
        return true;
    }

    @Override
    public String getContextMenuPage() {
        return "policy/query/contextMenu";
    }

    @Override
    public BaseModel getNodeById(String id) {
        return queryService.getQuery(id);
    }

    @Override
    public String getModifyPage() {
        return "policy/query/modify";
    }

    /**
     * 返回树的类型
     * @return
     */
    @Override
    public String getTreeType() {
        return "query";
    }

    @Override
    public boolean copyNode(Object node) {
        SafeQuery query = (SafeQuery) node;
        try{
            queryService.copyQuery(query.getId(), query.getName(), query.getDescription());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
